package com.sneaker.shower.config;


import com.sneaker.shower.config.socket.AutherImpl;
import com.sneaker.shower.config.socket.WsHandler;
import com.sneaker.shower.socket.Auther;
import com.sneaker.shower.socket.StandarChannel;
import com.sneaker.shower.socket.repository.ChannelRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * <h1>WsConfig</h1>
 * <pre>
 * @Description
 * </pre>
 *
 * @Author lopo
 * @Date 2018/10/19
 */
@Configuration
public class WsConfig extends WebMvcConfigurerAdapter implements WebSocketConfigurer {
    private ChannelRepository channelRepository;

    @Autowired
    public WsConfig(ChannelRepository channelRepository) {
        this.channelRepository = channelRepository;
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry webSocketHandlerRegistry) {
        webSocketHandlerRegistry.addHandler(new WsHandler(channelRepository), "/ws").setAllowedOrigins("*");
    }

    @Bean("auther")
    public Auther auther() {
        Auther auther = new AutherImpl();
        StandarChannel.setAuther(auther);
        return auther;
    }
}
